package com.yuanye.gwcit.utils;

import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class HttpHelper {

    private static final String tag = HttpHelper.class.getName();
    private static HttpHelper sInstance = null;

    public static HttpHelper getInstance() {
        if (sInstance == null) {
            synchronized (HttpHelper.class) {
                if (sInstance == null) {
                    sInstance = new HttpHelper();
                }
            }
        }
        return sInstance;
    }

    /**
     * HTTP GET请求
     * @param url 路径
     * @param callback 请求回调
     */
    public void get(String url, Callback callback){
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        client.newCall(request).enqueue(callback);
    }

    /**
     * HTTP POST请求
     * @param url 路径
     * @param json 待发送的json格式数据
     * @param callback 请求的回调
     */
    public static void post(String url, String json, Callback callback){
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(json, MediaType.parse("application/json;charset=utf-8"));
        Request request = new Request.Builder().url(url).post(body).build();
        client.newCall(request).enqueue(callback);
    }

}
